package com.terren.spider.core.html.scheme.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.transaction.TransactionStatus;

import com.terren.spider.core.html.configuration.Configuration;
import com.terren.spider.db.service.AppXhsService;
import com.terren.spider.entity.core.XhsVIPUser;
import com.terren.spider.util.common.CommonUtil;

public class XhsScheme extends BasicCustomScheme{
	private AppXhsService appXhsService;
	private List<XhsVIPUser> users;
	private Integer limit = 200;
	
	@Override
	public Set<String> findTargetUrl(Configuration configuration) {
		appXhsService = CommonUtil.getBean(AppXhsService.class);
		TransactionStatus tran = null;//开启事务
		Set<String> idSet=new HashSet<>();
		try {
			tran = appXhsService.startTransaction();//开启事务
			
			XhsVIPUser param = new XhsVIPUser();
			param.setLimit(limit);
			users=appXhsService.getAllXhsVIPUser(param);
			if (CollectionUtils.isNotEmpty(users)) {
				appXhsService.updateProgressStatus(users);// set  status = 9
				appXhsService.commitTransaction(tran);//提交事务
				
				for (int i = 0; i < users.size(); i++) {
					idSet.add("https://www.xiaohongshu.com/api/sns/v3/user/"+users.get(i).getUserid()+"/info?platform=Android&deviceId=14016bc0-f81b-3bd1-b3ab-33374d13223b&versionName=5.11.1&channel=Store360&sid=session.1211551853161779868&lang=zh-CN&t=1527474728&sign=79793e887a63b6b8589ad449a7636974");
				}
			}
			
				
		} catch (Exception e) {
			log.error("XhsScheme findTargetUrl error",e);
		} finally {
			if (tran!=null&&!tran.isCompleted()) {
				appXhsService.commitTransaction(tran);//提交事务
			}
		}
		return idSet;
	}
	@Override
	public void postFetch(Configuration configuration) {
		// TODO Auto-generated method stub
		if (CollectionUtils.isNotEmpty(users)) {
			try {
				appXhsService.updateDoneStatus(users);
				appXhsService.updateFetchtimes(users);
			} catch (Exception e) {
				log.error("XhsScheme postFetch error",e);
			}
		}
	}

}
